package com.ld.common.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ld.admin.model.system.SysMenuModel;
import com.ld.common.model.SortModel;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.reflect.ClassUtil;
import com.ld.shieldsb.common.core.reflect.ModelUtil;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;

@Service
public class SortService extends ProjectBasicService {

    /**
     * 树形结构排序
     * 
     * @Title resetTreeOrder
     * @author 吕凯
     * @date 2019年1月11日 上午11:07:54
     * @param type
     * @param asc
     *            是否升序，1是0否
     * @param resetorders
     * @param startIndex
     *            开始序号，分页时使用
     * @param fieldName
     * @return Result
     */
    public Result resetTreeOrder(SortModel sortModel) {
        List<Object> orderList = new ArrayList<Object>();
        if (!StringUtils.isBlank(sortModel.getSorts())) {
            String[] sorts = StringUtils.getStrArr(sortModel.getSorts());
            boolean isAsc = sortModel.getAsc();
            Integer startIndex = sortModel.getStartIndex();
            if (startIndex == null) { // 如果未设置
                if (isAsc) {
                    startIndex = 0;
                } else {
                    startIndex = sorts.length;
                }

            }
            if (sorts != null && sorts.length > 0) {
                String fieldName = sortModel.getFieldName();
                if (StringUtils.isBlank(fieldName)) {
                    fieldName = "sort";
                }
                for (int i = 0; i < sorts.length; i++) {
                    try {
                        String ordersStr = sorts[i];
                        if (!StringUtils.isBlank(ordersStr)) {
                            String[] orderVals = ordersStr.split("-"); // id-pid-level12
                            if (orderVals.length >= 3) {
                                long ordervalue = ClassUtil.obj2long(orderVals[0]);
                                long parentId = ClassUtil.obj2long(orderVals[1]);
                                int level = ClassUtil.obj2int(orderVals[2]);
                                Object model = getOrderModel(sortModel.getType());
                                ModelUtil.setModelValue(model, "id", ordervalue);
                                ModelUtil.setModelValue(model, "parentId", parentId);
                                ModelUtil.setModelValue(model, "treeLevel", level);
                                if (isAsc) { // 从小到大
                                    ModelUtil.setModelValue(model, fieldName, startIndex + i + 1); // 从1开始
                                } else {
                                    ModelUtil.setModelValue(model, fieldName, startIndex - i);
                                }
                                orderList.add(model);
                            }
                        }
                    } catch (NumberFormatException e) {
                        log.error("", e);
                        return Web.Response.error("排序失败！" + e.getMessage());
                    }
                }
                Result result = super.addOrUpdate(orderList, true);
                if (result.getSuccess()) {
                    return Web.Response.success("排序成功！");
                } else {
                    return Web.Response.success("排序失败！" + result.getMessage());
                }
            }
        }
        return Web.Response.error("非法操作！");

    }

    /**
     * 获取排序的model
     * 
     * @Title getOrderModel
     * @author 吕凯
     * @date 2019年1月11日 上午11:08:25
     * @param type
     * @return Object
     */
    private Object getOrderModel(int modelType) {
        Object model = null;
        if (modelType == 1) { // 系统菜单
            model = new SysMenuModel();
        }
        return model;
    }
}
